<template>
  <div>
    <div>
      <el-form :inline="true" :model="param">
        <el-col :span="7">
          <el-form-item label="分店名称">
            <el-input @clear="findBranch" v-model="param.branchName" placeholder="筛选分店" clearable
                      prefix-icon="el-icon-search"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item></el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" @click="findBranch">查询</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="success" @click="addBranch">新增</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </div>


    <div style="margin-bottom: 10px ;">
      <el-table ref="userTable" :data="tableData" :stripe="true" :border="true" style="width: 100%">
        <el-table-column prop="name" label="分店名称" width="200"></el-table-column>
        <el-table-column prop="createTime" label="添加时间"></el-table-column>
        <el-table-column prop="address" label="地址"></el-table-column>
        <el-table-column prop="administrator" label="分店管理员" width="100" align="center"></el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot="scope">
            <el-button @click="doDelete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background :page-sizes="[8, 16, 30]" @current-change="pageChange($event)"
                     :page-size=this.param.size @size-change="sizeChange($event)"
                     layout="total,prev,pager,next,sizes" :total="total" small>
      </el-pagination>


      <!--新增-->
      <el-dialog title="新增分店" :visible.sync="isAdding" width="30%" :close-on-click-modal="false" center>
        <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
          <el-form-item label="分店名称" prop="name">
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="addForm.address"></el-input>
          </el-form-item>
          <el-form-item label="管理员" prop="accountId">
            <el-select v-model="addForm.accountId" placeholder="请选择人员" clearable style="width: 100%">
              <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('addForm')">重置</el-button>
        <el-button type="primary" @click="add('addForm')">确认</el-button></span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "Branch",
  data() {
    return {
      userInfo: [],
      tableData: [],
      total: 0,
      isAdding: false,
      param: {
        branchName: '',
        page: 1,
        size: 8,
      },
      userList:{},
      addForm: {
        name: '',
        address: '',
        accountId:''
      },
      rules:{
        name: [
          {required: true, message: '请输入该选项', trigger: 'blur'},
          {min: 3, max: 10, message: '请输入3-10字', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入该选项', trigger: 'blur'},
          {min: 5, max: 20, message: '请输入5-20字', trigger: 'blur'}
        ],
        accountId: [
          {required: true, message: '请选择该选项', trigger: 'blur'},
        ],
      }
    }
  },
  mounted() {
    this.findBranch()
    this.findAdmin()
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"))
  },
  methods: {
    findBranch() {
      this.$axios.get('/api/house/findBranch', {params: this.param}).then(res => {
        if( res.data.status === 1){
          this.tableData = res.data.data.list
          this.total = res.data.data.total;
        }else {
          this.$notify({
            title: '提示',
            message: '查询失败',
            type: 'error'
          });
        }
      }).catch(e => console.log(e));
    },
    findAdmin(){
      this.$axios.get('/api/user/findUser' ).then(res => {
        console.log(res)
        this.userList = res.data.data.list;
      }).catch(e => console.log(e));
    },

    addBranch() {
      this.isAdding = true
    },
    add(addForm) {
      this.$refs[addForm].validate((valid) => {
        if (valid) {
          this.$axios.post('/api/house/addBranch', this.addForm).then(res => {
            if (res.data.status === 1) {
              this.$notify({
                title: '提示',
                message: '添加成功!',
                type: 'success'
              })
              this.isAdding = false;
              this.findBranch()
            } else {
              this.$notify({
                title: '提示',
                message: res.data.message,
                type: 'error'
              })
            }
          })
        }else {
          return false
        }
      })
    },
    doDelete(row) {
      this.$confirm('将要删除该店铺, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(row)
        let ids = row.id
        return this.$axios.delete(`/api/house/delBranch?ids=${ids}`).then(res => {  //物理删除
          if (res.data.status === 1) {  //删除成功
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success'
            });
            this.findBranch();
          } else {
            this.$notify({
              title: '提示',
              message: res.data.message,
              type: 'error'
            });
          }
        }).catch(e => e);
      })
    },

    down(row) {
      console.log(row)
      this.goods.id = row.id
      this.goods.status = 2
      this.upApi()
    },
    up(row) {
      this.goods.id = row.id
      this.goods.status = 1
      this.upApi()
    },
    upApi() {
      this.$axios.post('/api/com/upGoods', this.goods).then(res => {
        if (res.data.status === 1) {
          this.$notify({
            title: '提示',
            message: '操作成功',
            type: 'success'
          });
          this.findBranch()
        } else {
          this.$notify({
            title: '提示',
            message: res.data.message,
            type: 'error'
          });
        }
      })
    },


    resetForm(addForm) {
      this.$nextTick(() => {
        this.$refs[addForm].resetFields();
      })
    },
    pageChange(page) {
      this.param.page = page;
      this.findBranch()
    },
    sizeChange(size) {
      this.param.size = size;
      this.findBranch()
    },
  }

}
</script>

<style scoped>

</style>
